#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: vstore.py
# Author: Tomás Vírseda
# License: GPL v3
# Description:  This is the Virtual Store class. It is like a layer
# between the real store and the application. It keeps a cache of
# opened reosurces and update them when neccesary.

from datetime import datetime
import uuid

from rdflib import URIRef

from vazaarlib.namespaces import NIE, NAO, NFO, RDF, NSBINDINGS
from vazaarlib.utils import get_logger

class VirtualResource():
    """
    This is the Virtual Resource class.

    The Virtual Resource is a class which keeps in memory the metadata for a given resource:

    Common metadata:
    - identifier (like vazaar://efdbf7ff-c4cd-495f-93fb-a137f242c87a)
    - RDF.type a 'Nepomuk class'. (a small set of classes from several Nepomuk Ontologies like NIE, NFO, ...)
    - NAO['created']: created date (for stats)
    - NAO['lastModified']: lastmodified date (for stats)

    Common metadata for files:
    - NFO['fileExtension']: file extension (vazaar adds a triple even if not extension is given)
    - NIE['mimeType']: this is a complex issue:
        - Clipboard? add it as a NAO property and FILTER it in QUERIES? ask). FIXME.
        - Webpages content (eg.: images)? vazaar SHOULD add a triple. FIXME
    - NIE['mimeMedia']: left part of mimetype string
    - NIE['mimeSubtype']: right part of mimetype string
    - NFO['fileUrl']
    - NFO['fileName']
    -
    -
    -

    """

    def __init__(self):
        self.log = get_logger('VResource')
        self.id = None
        self.lastmodified = None
        self.prop = {}


    def get_id(self):
        return self.id


    def get_lastmodified(self):
        return self.lastmodified


    def set_lastmodified(self, date=None):
        """After changing any property lastmodified must be updated"""
        #FIXME: get date from store and compare it with vresource date.
        if date:
            self.lastmodified = date
        else:
            self.lastmodified = datetime.now()

        #self.log.debug("%s updated: %s" % (self.id, self.lastmodified))


    def set_id(self, id=None):
        """ Set resource identifier
            If id is None a random id will be generate. Otherway a
            specific id will be used.
        """

        if id is None:
            self.id = URIRef("vazaar://" + str(uuid.uuid4()))
            #self.prop['id'] = self.id
        else:
            self.id = URIRef(id)


    def set_metadata(self, lmetadata):
        """Recieve a list of tuples (predicate, object) and add them to
        the metadata dictionary

        Also, create shortcuts to common predicates: url, mime, name and
        type.
        """
        #self.prop['id'] = self.id
        for predicate, object in lmetadata:
            #Create shortcuts
            try:
                objects = self.prop[predicate]
                objects.append(object)
                self.prop[predicate] = objects
            except:
                objects = []
                objects.append(object)
                self.prop[predicate] = objects

        self.set_lastmodified()


    def get_metadata(self, predicate):
        """Return value for a given predicate for this resource"""

        return self.metadata[predicate]

    def describe(self):
        print "Resource: %s" % self.id
        print "\tLast modified: %s" % self.lastmodified
        for predicate, object in self.metadata:
            print "\t%s: %s" % (predicate, object)
